/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.test;

import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wttech.dzzf.check.moduleSet.CheckUserUpload_8;
import com.wttech.dzzf.process.dao.CbcBankDataCheckDao;
import com.wttech.dzzf.process.util.BusinessUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.wttech.dzzf.process.constant.UserConstants.*;

/**
 * 资金账户信息检测
 *
 * @author MaShibo
 * @date 2017/12/5.
 */
@Service
public class FundAccountCheck {


	private static final Logger USER = LoggerFactory.getLogger("User");

	@Resource
	private CbcBankDataCheckDao cbcBankDataCheckDao;

	@Resource
	private CardInfoCheck cardInfoCheck;

	public void check(FundAccount fundAccount) {
		checkFundAccount(fundAccount);
		List<CardInfo> cardInfos = cbcBankDataCheckDao.queryCardInfoByFundAccountId(fundAccount.getFundAccountId());
		cardInfos.forEach(this::checkCardInfo);
	}

	public void checkFundAccount(FundAccount fundAccount) {
		String fundAccountId = StringUtils.trim(fundAccount.getFundAccountId());
		CheckUserUpload_8 userCheck = new CheckUserUpload_8();
		int ownerType = fundAccount.getOwnerType();

		String result;

		int userType = ownerType == OLD_OWNERTYPE_USER ? DEFAULT_USER_USERTYPE : DEFAULT_COMPANY_USERTYPE;
		int certifiType = fundAccount.getCertifiType();
		int userIdType = BusinessUtils.convertUserIdType(certifiType, ownerType);
		String userName = fundAccount.getFundAccountName();
		String userIdNum = fundAccount.getCompanyLic();
//		String tel = StringUtils.isBlank(fundAccount.getPhone()) ? fundAccount.getMobile() : fundAccount.getPhone();
//		String address = BusinessUtils.regularString(fundAccount.getAddress());

		userCheck.setUserType(userType);
		userCheck.setUserIdType(userIdType);

		result = userCheck.checkUserType(userType);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			USER.info("资金账户【{}】，用户类型【{}】错误【{}】", new Object[]{fundAccountId, ownerType, result});
		}

		result = userCheck.checkUserName(userName);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			USER.info("资金账户【{}】，账户名称【{}】错误【{}】", new Object[]{fundAccountId, userName, result});
		}

		result = userCheck.checkUserIdType(userIdType);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			USER.info("资金账户【{}】，证件类型【{}】错误【{}】", new Object[]{fundAccountId, certifiType, result});
		}

		result = userCheck.checkUserIdNum(userIdNum);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			userCheck.setUserIdType(ownerType == OLD_OWNERTYPE_USER ? DEFAULT_USER_USERIDTYPE : DEFAULT_COMPANY_USRERIDTYPE);
			result = userCheck.checkUserIdNum(userIdNum);
			if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
				USER.info("资金账户【{}】， 证件号【{}】错误【{}】", new Object[]{fundAccountId, userIdNum, result});
			}
		}

		/*result = userCheck.checkTel(tel);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			USER.info("资金账户【{}】，手机号【{}】错误【{}】", new Object[]{fundAccountId, tel, result});
		}*/

		/*result = userCheck.checkAddress(address);
		if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
			USER.info("资金账户【{}】，地址【{}】错误【{}】", new Object[]{fundAccountId, address, result});
		}*/


/*		if (ownerType == OLD_OWNERTYPE_COMPANY) {
			String agentName = fundAccount.getRelationName();
			Integer agentIdType = BusinessUtils.convertUserIdType(fundAccount.getAgentIdType(), OLD_OWNERTYPE_USER);
			userCheck.setAgentIdType(agentIdType);
			String agentIdNum = fundAccount.getAgentIdNum();

			result = userCheck.checkAgentName(agentName);
			if (StringUtils.isNotBlank(agentName)) {
				if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
					USER.info("资金账户【{}】，经办人姓名【{}】错误【{}】", new Object[]{fundAccountId, agentName, result});
				}
			}


			result = userCheck.checkAgentIdType(agentIdType);
			if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
				USER.info("资金账户【{}】，经办人证件类型【{}】错误【{}】", new Object[]{fundAccountId, agentIdType, result});
			}

			if (StringUtils.isNotBlank(agentIdNum)) {
				result = userCheck.checkAgentIdNum(agentIdNum);
				if (!StringUtils.equalsIgnoreCase(result, RIGHT)) {
					USER.info("资金账户【{}】，经办人证件号【{}】错误【{}】", new Object[]{fundAccountId, agentIdNum, result});
				}
			}

		}*/
	}

	private void checkCardInfo(CardInfo cardInfo) {
		cardInfoCheck.checkCardInfo(cardInfo);
	}

}
